#include "DaterTransUser.h"	

namespace Dater
{
	template<> TransUserSearch* Singleton<TransUserSearch>::ms_Singleton = 0;

	bool TransUser::judge(const char* order)
	{
		split(order,_delimiter,_splitedOrder);
		return (_splitedOrder[0] == "4");
	}

	bool TransUserSearch::execute( int& clientUid, std::vector<std::string>& sendMessages, std::vector<int>& sendClientUids )
	{
		sendMessages.clear();
		sendClientUids.clear();
		sendClientUids.push_back(clientUid);

		std::string name;

		name = _splitedOrder[2];

		char SQL[400];
		std::string sqlResult;
		std::vector<std::string> dividedSqlResult1;
		std::vector<std::string> dividedSqlResult2;

		if(linktosql.ConnMySQL("localhost", "3306", "dater", (char*)_user.c_str(), (char*)_password.c_str() , "GBK", "") == 0)
			printf("Connect successfully\n");

		sprintf_s(SQL, "select uid, hypocorism, gender,	birthday from user where hypocorism like '%%%s%%';", name.c_str());
		sqlResult = linktosql.SelectData(SQL, 4, linktosql.Msg, _selectResultDelimiter, _delimiter);
		memset(SQL, 0, 400);

		split(sqlResult, _selectResultDelimiter, dividedSqlResult1);

		std::string sendMessage;
		sendMessage = "4\b0\b";

		sprintf_s(SQL,"%d",dividedSqlResult1.size());
		sendMessage += SQL;
		memset(SQL,0,400);

		for(unsigned i = 0; i < dividedSqlResult1.size(); i++)
		{
			split(dividedSqlResult1[i], _delimiter, dividedSqlResult2);
			sprintf_s(SQL,"\b%s\b%s\b%s\b%s",dividedSqlResult2[0].c_str(), dividedSqlResult2[1].c_str(),
				dividedSqlResult2[2].c_str(),dividedSqlResult2[3].c_str());
			sendMessage += SQL;
			memset(SQL,0,400);
		}

		sendMessages.push_back(sendMessage);
		linktosql.CloseMySQLConn();
		return true;
	}

	bool TransUserSearch::judge(const char* order)
	{
		if(!TransUser::judge(order))
			return false;
		else
			return (_splitedOrder[1] == "0");
	}
}